package cl.loso.melon.server.persistencia;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cl.loso.melon.server.gae.PMF;
import cl.loso.melon.server.model.NegocioLVS;
import cl.loso.melon.server.model.VisitaLVS;

public class NegocioLVSDAO {
	private static Log log = LogFactory.getLog(NegocioLVSDAO.class);
	
	/**
	 * @return una lista items
	 */
	@SuppressWarnings("unchecked")
	public static List<NegocioLVS> obtener() {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    List<NegocioLVS> negocioLst=null;
	    Query query=null;
	    try {
	    	query = pm.newQuery(NegocioLVS.class);
	    	query.setOrdering("nombre ASC");
	    	negocioLst = (List<NegocioLVS>)pm.detachCopyAll((List<NegocioLVS>)query.execute());
	    } catch (Exception e) {
	    	log.error(e.getMessage());	    	
	    } finally {
	        pm.close();
	    }
	    return negocioLst; 
	}	
	
	public static void insertar(NegocioLVS negociolvs) {

	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	    	pm.makePersistent(negociolvs);
	    } catch (Exception e) {
	    	log.error(e.getMessage());
	    } finally {
	        pm.close();
	    }			
	}	
	
	public static NegocioLVS getNegocioLVSbyId(Long id) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    NegocioLVS negocio, detached = null;
	    try {
	    	negocio = pm.getObjectById(NegocioLVS.class, id);
	        detached = pm.detachCopy(negocio);
	    } catch (Exception e) {
	    	log.error(e.getMessage());	        
	    } finally {
	        pm.close();
	    }
	    return detached;		

	}
	

	public static void actualizar(NegocioLVS newNegocioLVS) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	    	NegocioLVS negocio=pm.getObjectById(NegocioLVS.class, newNegocioLVS.getId());
	    	negocio.setNombre(newNegocioLVS.getNombre());

	    } catch (Exception e) {
	    	log.error(e.getMessage());	    	
	    } finally {
	        pm.close();
	    }		

	}	
	
	public static void borrar(Long id) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	    	pm.deletePersistent(pm.getObjectById(NegocioLVS.class, id));
	    } catch (Exception e) {
	    	log.error(e.getMessage());	    	
	    } finally {
	        pm.close();
	    }		
	}	
	
	public static List<NegocioLVS> buscarNombre(String pnombre) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    List<NegocioLVS> detached = null;

	    try {
	    	Query query = pm.newQuery(NegocioLVS.class,"nombre == pnombre");
	    	query.declareParameters("String pnombre");

	    	detached = (List<NegocioLVS>)pm.detachCopyAll((List<NegocioLVS>) query.execute(pnombre));
  
	    } catch (Exception e) {
	    	log.error(e.getMessage());	        
	    } finally {
	        pm.close();
	    }
	    return detached;		

	}	
	
	public static boolean existeVisita(String idNegocio) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = null;
		List<VisitaLVS> visitas = null;
		try {
			Map<String, Object> pars = new HashMap<String, Object>();
			String queryStr = "negocio == :neg";
			pars.put("neg", Long.valueOf(idNegocio));
			query = pm.newQuery(VisitaLVS.class);
			query.setFilter(queryStr);
			visitas = (List<VisitaLVS>) pm
					.detachCopyAll((List<VisitaLVS>) query.executeWithMap(pars));
			if(!visitas.isEmpty()) return true;
			
	    } catch (Exception e) {
	    	log.error(e.getMessage());	    	
	    } finally {
	        pm.close();
	    }
	    return false; 
	}	
}
